package java_collection;

public class Circle {
	private int radius;
	
	public Circle(int radius) {
		this.radius = radius;
	}
	
	public String toString(){
		return "Radius : " + radius;
	}
	
	
	public boolean equals(Object obj) {
		Circle other = (Circle) obj;
		
		// Req #1, null test
		// For any non-null reference value x, x.equals(null) should return false
		if (obj == null) {
			return false;
		}
		
		// Req #2, It is reflexive
		//  x.equals(x) should return true
		if (this == obj) {
			return true;
		}
		
		// Req #3, It is symmetric
		//  x.equals(y) should return true if and only if y.equals(x) returns true
		// Req #4, It is transitive
		// If x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true
		if (Double.doubleToLongBits(radius) != Double.doubleToLongBits(other.radius)) {
			return false;
		}
		
		// Req 5, Avoid Objects of sub classes to be equal to objects of parent class
		// Don't use instanceof
		
		/*if ((this instanceof Circle && other instanceof Circle)) {
			// OK
		}else {
			return false;
		}*/
		
		if (this.getClass() != other.getClass()) {
			return false;
		}
		
		return true;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + radius;
		return result;
	}
	
}
